package com.chookss.home.knowledge.adaper;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.document.DocumentBaseDirectorySecondActivity;
import com.chookss.home.entity.KnowledgeEntity;
import com.chookss.home.knowledge.KnowledgeBaseSecondActivity;
import com.chookss.tools.Utils;
import com.chookss.view.flowLayout.FlowFoldLayout;
import com.chookss.view.listener.ShareDialogListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

public class MyDirectoryAdapter extends BaseQuickAdapter<KnowledgeEntity, BaseViewHolderEx> {
    private Context context;
    private int type;//0知识库目录 2资料库目录
    private String resourceType = "";////        资源类型（1资料，2长视频，3短视频 6图片）
    public MyDirectoryAdapter(int layoutResId, Context context, @Nullable List<KnowledgeEntity> data, int type,String resourceType) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        this.resourceType = resourceType;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, KnowledgeEntity item) {
        RelativeLayout rlAll = helper.getView(R.id.rlAll);
        ImageView ivIconType = helper.getView(R.id.ivIconType);
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvNum = helper.getView(R.id.tvNum);
        View spaceView = helper.getView(R.id.spaceView);
        RelativeLayout rlFlowLayout = helper.getView(R.id.flowLayout);
        tvNum.setText(Utils.changeNumber(item.getFileNum())+"");

        if(type==0){
            //知识库目录
            ivIconType.setVisibility(View.VISIBLE);
            if ("1".equals(item.getCode())) {
                Glide.with(context).load(R.drawable.zsk_gxkj_0).into(ivIconType);
            } else if ("2".equals(item.getCode())) {
                Glide.with(context).load(R.drawable.zsk_wdbm_0).into(ivIconType);
            } else if ("3".equals(item.getCode())) {
                Glide.with(context).load(R.drawable.zsk_grwj_0).into(ivIconType);
            } else {
                Glide.with(context).load(R.drawable.zsk_zdy_0).into(ivIconType);
            }
            tvTitle.setText(item.getName()+"("+item.getFileNum()+")");
            tvNum.setVisibility(View.GONE);
            rlAll.setBackgroundResource(R.drawable.shape_directory);
        }else{
            ivIconType.setVisibility(View.GONE);
            tvTitle.setText(item.getName());
            tvNum.setVisibility(View.VISIBLE);
        }
        if (item.getChildList().size() == 0) {
            rlFlowLayout.setVisibility(View.GONE);
            spaceView.setVisibility(View.VISIBLE);
        } else {
            rlFlowLayout.setVisibility(View.VISIBLE);
            spaceView.setVisibility(View.GONE);
            KnowledgeEntity levelOne=item;
            levelOne.setResourceType(resourceType);
            rlFlowLayout.removeAllViews();
            FlowFoldLayout flowFoldLayout=new FlowFoldLayout(context);
            flowFoldLayout.setFold(true);
            flowFoldLayout.setFoldLines(2);
            flowFoldLayout.setEqually(false);
            flowFoldLayout.setVerticalSpacing(Utils.dip2px(context,1));
            flowFoldLayout.setHorizontalSpacing(Utils.dip2px(context,17));
            FlowMoreAdapter adapter = new FlowMoreAdapter();
            adapter.setData(context, levelOne, type, "");
            adapter.setNewData(item.getChildList());
            flowFoldLayout.setAdapter(adapter);
            flowFoldLayout.setMoreClick(new ShareDialogListener() {
                @Override
                public void onClick() {
                    toSecond(item);
                }
            });
            rlFlowLayout.addView(flowFoldLayout);
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSecond(item);
            }
        });
    }
    private void toSecond(KnowledgeEntity item){

        //自定义不能点击
        if (type == 0) {
            //知识库
            Intent intent = new Intent(context, KnowledgeBaseSecondActivity.class);
            intent.putExtra("parentCode", item.getCode());
            intent.putExtra("folderNum", item.getFolderNum());
            intent.putExtra("levelOne", item);
            intent.putExtra("resourceType", resourceType);
            if("0".equals(item.getCode())){
                intent.putExtra("catalogType", "-1");
            }else{
                intent.putExtra("catalogType", "0");
            }
            intent.putExtra("level", 1);
            ArrayList<String> nameList = new ArrayList<>();//组织排列
            nameList.add(item.getName());
            intent.putStringArrayListExtra("nameList", nameList);
            context.startActivity(intent);
        } else    if (type == 2){
            //资料库
            Intent intent = new Intent(context, DocumentBaseDirectorySecondActivity.class);
            intent.putExtra("parentCode", item.getCode());
            if("0".equals(item.getCode())){
                intent.putExtra("catalogType", "3");
            }else{
                intent.putExtra("catalogType", "0");
            }
            intent.putExtra("level", 1);
            intent.putExtra("levelOne", item);
            intent.putExtra("folderNum", item.getFolderNum());
            ArrayList<String> nameList = new ArrayList<>();//组织排列
            nameList.add(item.getName());
            intent.putStringArrayListExtra("nameList", nameList);
            context.startActivity(intent);
        }

    }
    public void setResourceType(String resourceType){
        this.resourceType=resourceType;
    }
}
